package com.laxcen.basiclib.core.task.core.thread;

import android.text.TextUtils;


import com.laxcen.basiclib.core.task.ThreadPriority;
import com.laxcen.basiclib.core.task.core.task.TaskHelper;

import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/** 
 * 自定义线程池
 */

class ThreadPool extends AbstractExecutorService implements IExecutorService {
    private final TaskHelper mTaskHelper;

    /**
     *
     * @param coreSize 核心数
     * @param groupName 线程池组名称
     * @param priorit 线程优先级 {@link Thread}
     */
    public ThreadPool(int coreSize, String groupName, int priorit) {
        if (priorit <= 0) {
            priorit = Thread.NORM_PRIORITY;
        }
        mTaskHelper = new TaskHelper(coreSize, groupName, priorit);
    }


    @Override
    public void execute(Runnable command) {
        execute(command, "", 0, ThreadPriority.MIN_PRIORITY);
    }


    @Override
    public void execute(Runnable command, String tag, int delayTime, int priority) {
        if (TextUtils.isEmpty(tag)) {
            mTaskHelper.add(command);
        } else {
            mTaskHelper.add(command, tag.hashCode(), 0, priority);
        }
    }

    /**
     * 根据tag移除线程池中的任务
     * @param tag
     */
    @Override
    public void removeAllByTag(String tag) {
        mTaskHelper.removeAllByTag(tag.hashCode());

    }

    /**
     * 移除线程池中所有任务
     */
    @Override
    public void removeCallbacksAndMessages() {
        mTaskHelper.removeCallbacksAndMessages();
    }

    @Override
    public ExecutorService getExecutorService() {
        return this;
    }

    @Override
    public void shutdown() {

    }

    @Override
    public List<Runnable> shutdownNow() {
        return null;
    }

    @Override
    public boolean isShutdown() {
        return false;
    }

    @Override
    public boolean isTerminated() {
        return false;
    }

    @Override
    public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException {
        return false;
    }
}

